
package com.plian.system.service.fr.payrolladjustment.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.fr.payrolladjustment.PayrollAdjustmentDao;
import com.plian.system.entity.fr.payrolladjustment.PayrollTableConfig;
import com.plian.system.mapper.fr.payrolladjustment.PayrollTableConfigMapper;
import com.plian.system.service.fr.payrolladjustment.IPayrollTableConfigService;
import com.plian.system.service.fr.payrolladjustment.IPayrollVersionService;
import com.plian.system.vo.fr.report.ReportTableConfigVO;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 表格配置 前端用 服务实现类
 *
 * @author jianglei
 * @since 2022-02-08
 */
@Service
@AllArgsConstructor
public class PayrollTableConfigServiceImpl extends ServiceImpl<PayrollTableConfigMapper, PayrollTableConfig> implements IPayrollTableConfigService {

    private IPayrollVersionService payrollVersionService;

    private PayrollAdjustmentDao payrollAdjustmentDao;

    /**
     * logger属性.
     */
    private static final Logger LOGGER  = LoggerFactory.getLogger(PayrollTableConfigServiceImpl.class);

    private static String VERSION = "version";

    @Override
    public ReportTableConfigVO getTableConfig(String year) {
        PayrollTableConfig payrollTableConfig = getOne(new LambdaQueryWrapper<PayrollTableConfig>().eq(PayrollTableConfig::getYear, year));
        List<PageData> list = new ArrayList<>();
        try {
            PageData pd = new PageData();
            pd.put(VERSION, payrollVersionService.getVersion(year));
            list = payrollAdjustmentDao.listAllOrg(pd);
        }catch (Exception e){
            LOGGER.error("" + e);
        }

        ReportTableConfigVO vo = ReportTableConfigVO.builder()
                .tableConst(JSONArray.parseArray(payrollTableConfig.getTableConst()))
                .tableNames(JSON.parseArray(payrollTableConfig.getTableNames()))
                .colNames(JSON.parseArray(payrollTableConfig.getColNames()))
                .required(JSON.parse(payrollTableConfig.getRequired()))
                .rule(JSON.parseArray(payrollTableConfig.getRule()))
                .reportOrgList(list)
                .build();

        return vo;
    }

}
